#pragma once

namespace BFX
{

// This abstract base class represents a writer that can write a sequential stream of characters.
// A subclass must minimally implement the Write(char) method.
class BFX_API REFUniTextWriter : public REFObject
{
public:
	REFUniTextWriter();
	virtual ~REFUniTextWriter();

	// Closes this text writer and releases any system resources associated with it.
	virtual void Close() {}
	// Clears all buffers for this TextWriter and causes any buffered data to be written to the underlying device.
	virtual void Flush() {}
	// Writes a character to the text stream.
	virtual void Write(UCHAR ch) {}
	// Writes a character array to the text stream.
	virtual void Write(SequenceT<UCHAR> buffer)
	{
		if (buffer.GetSize() > 0)
			Write(buffer.GetData(), 0, buffer.GetSize());
	}
	// Writes a range of a character array to the text stream.
	virtual void Write(UCSTR pchBuffer, int iOffset, int nLength)
	{
		BFX_REQUIRE (pchBuffer != NULL || nLength == 0);
		BFX_REQUIRE (iOffset >= 0 && nLength >= 0);
		for (int i = 0; i < nLength; i ++)
			Write(pchBuffer[iOffset + i]);
	}
	// Writes a string to the text stream.
	virtual void Write(const UniString& strBuffer)
	{
		if (!strBuffer.IsEmpty())
			Write(strBuffer, 0, strBuffer.GetLength());
	}
	// Writes a line terminator to the text stream
	virtual void WriteLine()
	{
		Write(s_pszlNewLine);
	}
	// Writes an array of characters followed by a line terminator to the text stream.
	virtual void WriteLine(SequenceT<UCHAR> buffer)
	{
		Write(buffer);
		WriteLine();
	}
	// Writes a range of a character array followed by a line terminator to the text stream.
	virtual void WriteLine(UCSTR pchBuffer, int iOffset, int nLength)
	{
		Write(pchBuffer, iOffset, nLength);
		WriteLine();
	}
	// Writes a string followed by a line terminator to the text stream.
	virtual void WriteLine(const UniString& strBuffer)
	{
		Write(strBuffer);
		WriteLine();
	}
private:
	static const UCHAR	s_pszlNewLine[];
};

// This class implements a text writer that writes to a string buffer and allows the resulting
// sequence of characters to be presented as a string.
class BFX_API REFUniStringWriter : public REFUniTextWriter
{
public:
	REFUniStringWriter();
	virtual ~REFUniStringWriter();

	// Writes a character to the text stream.
	virtual void Write(UCHAR ch);
	// Writes a range of a character array to the text stream.
	virtual void Write(UCSTR pchBuffer, int iOffset, int nLength);
	// Writes a string to the text stream.
	virtual void Write(const UniString& strBuffer);

	UniString ToString() const;

private:
	UniString	m_strBuffer;
};

}	//	namespace BFX
